package net.melon.cata.pojo;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Transient;

import net.esj.basic.pojo.AbstractPojo;
import net.esj.basic.pojo.TreeStructure;
import net.esj.basic.utils.StringUtils;


public abstract class Catalog<T extends Catalog> extends AbstractPojo<Long> implements TreeStructure{

	public static final String CATALOG_SERVICE_POSTFIX = "CatalogService";
	
	public static final String COLUMN_ID = "ID";
	public static final String COLUMN_NAME = "NAME";
	public static final String COLUMN_NAME_SUB = "NAME_SUB";
	public static final String COLUMN_DURABLE = "DURABLE";
	
	private Long id;
	private String name;
	private String nameSub;
	protected T parent;
	private boolean durable;
	
	private Set<T> children = new HashSet<T>(0);
	
	private boolean hasChildren;
	
	private String fullName;
	
	public Catalog() {
		super();
	}

	public Catalog(Long id) {
		super();
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNameSub() {
		return nameSub;
	}

	public void setNameSub(String nameSub) {
		this.nameSub = nameSub;
	}

	public boolean isDurable() {
		return durable;
	}

	public void setDurable(boolean durable) {
		this.durable = durable;
	}

	public T getParent() {
		return parent;
	}

	public void setParent(T parent) {
		this.parent = parent;
	}

	public Set<T> getChildren() {
		return children;
	}

	public void setChildren(Set<T> children) {
		this.children = children;
	}

	@Override
	public <T extends TreeStructure> Collection<T> getChildNodes() {
		return (Collection<T>) children;
	}

	@Override
	public String getNodeClsIcon() {
		return null;
	}

	@Override
	public Serializable getNodeId() {
		return id;
	}

	@Override
	public Integer getNodeLoadType() {
		return TREE_LOAD_TYPE_ON_CLICK_PARENT;
	}

	@Override
	public String getNodeTitle() {
		return name;
	}

	@Override
	public TreeStructure getParentNode() {
		return parent;
	}

	@Override
	public Boolean hasCheckBox() {
		return false;
	}

	@Override
	public Boolean hasChildNode() {
		return (hasChildren || !children.isEmpty());
	}

	@Override
	public Boolean isNodeExpansion() {
		return true;
	}

	@Override
	public Boolean isSelectCascade() {
		return false;
	}

	@Transient
	public String getFullName() {
		return fullName;
	}

	public void setFullName(String[] parentNames,String split) {
		StringBuilder sb = new StringBuilder();
		for(int i=parentNames.length-1;i>=0;i--){
			if(StringUtils.hasText(parentNames[i])){
				sb.append(parentNames[i]);
				sb.append(split);
			}
		}
		sb.append(getName());
		this.fullName = sb.toString();
	}

	public void setHasChildren(boolean hasChildren) {
		this.hasChildren = hasChildren;
	}

}
